﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DGSFramework;

namespace Framework.API.Advertising
{
    public partial class Default : System.Web.UI.Page
    {
        DGSFrameworkEntities entity = new DGSFrameworkEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["code"] != null && Request.QueryString["id"] != null && Request.QueryString["link"] != null && Request.QueryString["ref"] != null)
            {
                string code = Request.QueryString["code"].ToString();
                int id = 0;
                int.TryParse(Request.QueryString["id"].ToString(), out id);
                string link = HttpUtility.UrlDecode(Request.QueryString["link"].ToString());
                string refLink = HttpUtility.UrlDecode(Request.QueryString["ref"].ToString());

                var advertising = entity.udv_Advertise.Where(ad => ad.AdvID == id).FirstOrDefault();
                if (advertising != null)
                {
                    string code2 = Globals.EncryptMD5(advertising.AdvID.ToString() + advertising.AdvLink.ToLower() + ConfigPaths.Ref_Advertise360.ToLower()).ToLower();
                    if (code == code2 && link == advertising.AdvLink)
                    {
                        string ipAdress = Globals.GetClientIPAddress();
                        HttpBrowserCapabilities browserInfo = Request.Browser;
                        string browser = browserInfo.Type.ToLower() + " | " + browserInfo.Platform.ToLower();

                        udv_ReportAdvertise reportAdvertise = entity.udv_ReportAdvertise
                            .Where(ra =>
                                ra.IPAdress == ipAdress &&
                                ra.Browser == browser &&
                                ra.RefLink == refLink &&
                                ra.AddedDate.Value.Day == DateTime.Now.Day &&
                                ra.AddedDate.Value.Month == DateTime.Now.Month &&
                                ra.AddedDate.Value.Year == DateTime.Now.Year).FirstOrDefault();
                        if (reportAdvertise == null)
                        {
                            reportAdvertise = new udv_ReportAdvertise();
                            reportAdvertise.IPAdress = ipAdress;
                            reportAdvertise.Browser = browser;
                            reportAdvertise.udv_Advertise = advertising;
                            reportAdvertise.AddedDate = DateTime.Now;
                            reportAdvertise.RefLink = refLink;
                            entity.AddToudv_ReportAdvertise(reportAdvertise);
                            entity.SaveChanges();
                        }
                        Response.Redirect(advertising.AdvLink.Trim());
                    }
                    else
                    {
                        Response.Write("Quảng cáo không tồn tại");
                    }
                }
                else
                {
                    Response.Write("Quảng cáo không tồn tại");
                }
            }
            else
            {
                Response.Write("Quảng cáo không tồn tại");
            }
        }
    }
}